// SPDX-FileCopyrightText: 2023-2025 Sangfor Technologies Inc.
// SPDX-License-Identifier: Mulan PSL v2
package resource

import (
	"testing"

	. "github.com/smartystreets/goconvey/convey"

	"code.geniusai.org/notebook-manager/pkg/entity"
	"code.geniusai.org/notebook-manager/pkg/log"
)

func TestCreateNotebook(t *testing.T) {

	Convey("create notebook", t, func() {

		manager := getTestManager()
		notebook := &entity.Notebook{
			Name:   "unit-test",
			UserID: "default",
			ModifyNotebook: entity.ModifyNotebook{
				Image:       "registry.ai.geniusai.com/gcr.io/kubeflow-images-public/tensorflow-1.15.2-notebook-cpu:1.0.0",
				CpuNum:      1,
				VGpuNum:     0,
				MemorySize:  5,
				Description: "这里描述信息： 0918234091845klsd",
			},
			ProjectID:     "unit_hello",
			NewVolume:     true,
			WorkspaceSize: 3,
			VolumeName:    "pvc-unit-test",
			//VolumeMounts: []entity.VolumeMount{
			//	entity.VolumeMount{
			//		Name: "unit-test",
			//		IsNew: false,
			//		MountPath: "/unit-test",
			//		Size: 1,
			//	},
			//	entity.VolumeMount{
			//		Name: "unit-test-2",
			//		IsNew: true,
			//		MountPath: "/unit-test-2",
			//		Size: 1,
			//	},
			//},
		}
		nb, err := manager.CreateNotebook(notebook)
		if err != nil {
			t.Logf("create error: %v", err)
		}
		So(err, ShouldBeEmpty)
		So(nb, ShouldNotBeEmpty)
	})
}

func TestModifyNotebook(t *testing.T) {

	Convey("modify notebook", t, func() {

		manager := getTestManager()
		modifyNotebook := &entity.ModifyNotebook{
			Image:      "registry.ai.geniusai.com/gcr.io/kubeflow-images-public/tensorflow-1.15.2-notebook-cpu:1.0.0",
			CpuNum:     1,
			VGpuNum:    0,
			MemorySize: 1,
		}
		nb, err := manager.ModifyNotebook("default_unit-test_unit_hello", modifyNotebook)
		if err != nil {
			t.Logf("update error: %v", err)
		}
		So(err, ShouldBeEmpty)
		So(nb, ShouldNotBeEmpty)
	})
}

func TestListNotebook(t *testing.T) {
	log.Init()
	Convey("list notebook", t, func() {

		manager := getTestManager()
		total, list, err := manager.ListNotebook(&entity.ProjectListOption{UserID: "default", ProjectID: "unit_hello"})
		if err != nil {
			t.Logf("list notebook error: %v", err)
		}
		for _, item := range list {
			t.Logf("notebooks is: %+v", item)
		}
		So(total, ShouldBeGreaterThanOrEqualTo, 1)
		So(list, ShouldNotBeEmpty)
		So(err, ShouldBeEmpty)
	})
}

func TestStartNotebook(t *testing.T) {
	Convey("start notebook", t, func() {

		manager := getTestManager()
		err := manager.StartNotebook("default_unit-test_unit_hello")
		if err != nil {
			t.Logf("start error: %v", err)
		}
		So(err, ShouldBeEmpty)
	})
}

func TestActiveNotebook(t *testing.T) {
	Convey("start notebook", t, func() {

		manager := getTestManager()
		err := manager.StartNotebook("default_unit-test_unit_hello")
		if err != nil {
			t.Logf("start error: %v", err)
		}
		So(err, ShouldBeEmpty)
	})
}

func TestStopNotebook(t *testing.T) {
	Convey("stop notebook", t, func() {

		manager := getTestManager()
		err := manager.StopNotebook("default_unit-test_unit_hello")
		if err != nil {
			t.Logf("stop error: %v", err)
		}
		So(err, ShouldBeEmpty)
	})
}

func TestDeleteNotebook(t *testing.T) {
	Convey("delete notebook", t, func() {

		manager := getTestManager()
		err := manager.DeleteNotebook("default_unit-test_unit_hello")
		if err != nil {
			t.Logf("delete error: %v", err)
		}
		So(err, ShouldBeEmpty)
	})
	// TODO delete running notebook
}

func init() {
	log.Init()
}
